package com.yang.yangListener;

import com.yang.YangCuiLian;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.FurnaceBurnEvent;
import org.bukkit.event.inventory.FurnaceSmeltEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class YangListener implements Listener {

    private YangCuiLian yangCuiLian;

    public YangListener(YangCuiLian yangCuiLian) {
        this.yangCuiLian = yangCuiLian;
    }

    //    @EventHandler
//    public void furnaceSmeltEvents(FurnaceBurnEvent furnaceBurnEvent){
//        yangCuiLian.getLogger().info("1111");
//        ItemStack fuel = furnaceBurnEvent.getFuel();
//        yangCuiLian.getLogger().info(String.valueOf(fuel));
//        furnaceBurnEvent.setBurnTime(1000);
//        furnaceBurnEvent.setBurning(true);
//    }
    @EventHandler
    public void onFurnaceSmelt(FurnaceSmeltEvent event) {
        ItemStack smeltedItem = event.getResult();
        if (isCustomSword(smeltedItem)) {
            applyCustomEnchantments(smeltedItem);
        }
    }

    @EventHandler
    public void onFurnaceBurn(FurnaceBurnEvent event) {
        ItemStack burningItem = event.getFuel();
        if (isCustomSword(burningItem)) {
            // 检查熔炉中的燃料是否为剑，你可以在这里执行其他逻辑
            yangCuiLian.getLogger().info("1");
        }
    }

    private boolean isCustomSword(ItemStack item) {
        return item.getType() == Material.DIAMOND_SWORD; // 你可以根据需要修改判断条件
    }

    private void applyCustomEnchantments(ItemStack item) {
        Random random = new Random();
        int enchantmentLevel = 1; // 这里可以根据淬炼等级来设置

        // 示例属性：物理伤害
        double physicalDamage = enchantmentLevel * 1.5;

        // 示例属性：燃烧几率
        double burnChance = 0.1 * enchantmentLevel + random.nextDouble();

        // 示例属性：燃烧伤害
        double burnDamage = 2.0 * enchantmentLevel;

        // 在这里将计算得到的属性值应用到物品上，比如添加 Lore
        ItemMeta itemMeta = item.getItemMeta();
        if (itemMeta != null) {
            itemMeta.addEnchant(Enchantment.DAMAGE_ALL, enchantmentLevel, true);
            itemMeta.setDisplayName("Custom Sword");
            // 添加其他属性的 Lore
            itemMeta.setLore(getCustomLore(physicalDamage, burnChance, burnDamage));

            // 更新物品的元数据
            item.setItemMeta(itemMeta);
        }
    }

    private List<String> getCustomLore(double physicalDamage, double burnChance, double burnDamage) {
        List<String> lore = new ArrayList<>();
        lore.add("§e物理伤害: §7" + physicalDamage);
        lore.add("§e燃烧几率: §7" + burnChance);
        lore.add("§e燃烧伤害: §7" + burnDamage);
        // 添加其他属性的 Lore

        return lore;
    }
}
